package com.mg.framework.kernel;

import java.io.IOException;
import java.io.InputStream;

import android.content.res.AssetManager;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;

import com.mg.framework.tiled.Map;
import com.mg.framework.tiled.TMXMapReader;

public class Assets {

	public static Bitmap logo;
	public static Bitmap start;
	public static Bitmap exit;
	public static Bitmap moveC;
	public static Bitmap okC;
	public static Bitmap cancelC;
	public static Bitmap menuC;

	public static MG mg;
	private static AssetManager assetManager;
	private static TMXMapReader mapReader;
	private static String appBasePath;

	static void initAssets(MG mg) {
		Assets.appBasePath = "app/";
		Assets.mg = mg;
		Assets.assetManager = mg.getAssets();
		Assets.mapReader = new TMXMapReader(assetManager);
	}

	static void loadingInitAssets() {
		Assets.logo = Assets.loadingAssets(appBasePath + "Glogo.png");
		Assets.start = Assets.loadingAssets(appBasePath + "start.png");
		Assets.exit = Assets.loadingAssets(appBasePath + "exit.png");
		Assets.moveC = Assets.loadingAssets(appBasePath + "MoveC.png");
		Assets.okC = Assets.loadingAssets(appBasePath + "OkC.png");
		Assets.cancelC = Assets.loadingAssets(appBasePath + "CancelC.png");
		Assets.menuC = Assets.loadingAssets(appBasePath + "MenuC.png");
	}

	static Bitmap loadingAssets(String fileName) {
		Bitmap bitmap = null;
		try {
			InputStream in = assetManager.open(fileName);
			bitmap = BitmapFactory.decodeStream(in).copy(
					Bitmap.Config.ARGB_8888, true);
			in.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return bitmap;
	}

	static Map loadingTiledMapAssets(String basePath, String fileName,
			String tileObjectPackageName) {
		Map map = null;
		try {
			InputStream in = assetManager.open(basePath + fileName);
			map = mapReader.readMap(basePath, in, tileObjectPackageName);
			in.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return map;
	}

	static Bitmap[] loadingAssets(String[] fileName) {
		Bitmap[] bitmaps = new Bitmap[fileName.length];
		try {
			for (int i = 0; i < fileName.length; i++) {
				InputStream in = assetManager.open(fileName[i]);
				bitmaps[i] = BitmapFactory.decodeStream(in).copy(
						Bitmap.Config.ARGB_8888, true);
				in.close();
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
		return bitmaps;
	}
}